package com.edu.web.controller.student;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.edu.common.annotation.EditGroup;
import com.edu.common.annotation.Log;
import com.edu.common.core.controller.BaseController;
import com.edu.common.core.domain.AjaxResult;
import com.edu.common.core.page.TableDataInfo;
import com.edu.common.enums.BusinessType;
import com.edu.common.utils.poi.ExcelUtil;
import com.edu.system.domain.Student;
import com.edu.system.domain.StudentTag;
import com.edu.system.domain.dto.StudentDTO;
import com.edu.system.service.IStudentService;
import com.edu.system.service.IStudentTagService;

/**
 * 【学生管理】Controller
 * 
 * @author xxxx
 * @date 2023-12-16
 */
@RestController
@RequestMapping("/student")
public class StudentController extends BaseController
{
    @Autowired
    private IStudentService studentService;
    @Autowired
    private IStudentTagService studentTagService;

    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('student:list')")
    @GetMapping("/list")
    public TableDataInfo list(Student student)
    {
        startPage();
        List<StudentDTO> list = studentService.showStudentList(student);
        return getDataTable(list);
    }
    
    @PreAuthorize("@ss.hasPermi('student:listByTags')")
    @GetMapping("/listByTags")
    public TableDataInfo listByTags(Student student)
    {
        startPage();
        if(null != student.getTags() && !student.getTags().isEmpty()) {
        	List<StudentTag> tags = studentTagService.selectStuNosByTag(StudentTag.builder().tagKeys(student.getTags()).build());
        	List<String> stuNos = tags.stream().map(row->row.getStuNo()).collect(Collectors.toList());
        	student.setStuNos(stuNos);
        }
        List<StudentDTO> list = studentService.showStudentList(student);
        return getDataTable(list);
    }


    /**
     * 导出学生列表
     */
    @PreAuthorize("@ss.hasPermi('student:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.exportExcel(response, list, "【学生】数据");
    }

    /**
     * 导入
     * @param file
     * @return
     * @throws Exception
     */
    @Log(title = "学生", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('student:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        List<Student> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = studentService.importStudent(userList, operName);
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<Student> util = new ExcelUtil<Student>(Student.class);
        util.importTemplateExcel(response, "学生数据");
    }

    /**
     * 获取学生详细信息
     */
    @PreAuthorize("@ss.hasPermi('student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(studentService.selectStudentById(id));
    }

    /**
     * 新增学生
     */
    @PreAuthorize("@ss.hasPermi('student:add')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(EditGroup.class) Student student)
    {
        return toAjax(studentService.insertStudent(student));
    }

    /**
     * 修改学生
     */
    @PreAuthorize("@ss.hasPermi('student:edit')")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(EditGroup.class) Student student)
    {
        return toAjax(studentService.updateStudent(student));
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@ss.hasPermi('student:remove')")
    @Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(studentService.deleteStudentByIds(ids));
    }

    /**
     * 搜索学生列表
     */
    @GetMapping("/searchStudent")
    public AjaxResult searchStudent(Student student)
    {
        List<Student> list = studentService.selectStudentList(student);
        return success(list);
    }

    @GetMapping(value = "/cnt")
    public AjaxResult getCnt() {
        return success(studentService.getStudentCnt());
    }

}
